package com.authine.cloudpivot.ext.applicationservice;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.SaleSupportRecordDto;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.common.util.ObjectMapperUtils;
import com.authine.hermes.app.launcher.service.ApplicationService;
import com.authine.mvp.app.launcher.domain.AttachmentFormat;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

@Slf4j
@Path("SaleSupportRecordService")
public class SaleSupportRecordService extends ApplicationService {

    @Path("saveSaleSupportRecord")
    @POST
    @Operation(summary = "售后登记-添加处理记录",tags = CUSTOM_SERVICE)
    public SingleResponse saveSaleSupportRecord(@Parameter SaleSupportRecordDto saleSupportRecordDto){
        try{
            if (StringUtils.isBlank(saleSupportRecordDto.getSaleSupportRegisterId()) || StringUtils.isBlank(saleSupportRecordDto.getRecordContent())){
                return com.alibaba.cola.dto.SingleResponse.error("-1","缺少参数");
            }
            Map<String, Object> map = BeanUtil.beanToMap(saleSupportRecordDto);
            BoServiceUtils.createBo(ModelConst.SALE_SUPPORT_REGISTER_RECORD, Collections.singletonList(map));
            if (saleSupportRecordDto.getAttachmentFormats() != null){
                //更新售后登记表附件
                Map<String,Object> param = new HashMap<>();
                com.alibaba.cola.dto.SingleResponse<BO> saleSupport = BoServiceUtils.loadBo(saleSupportRecordDto.getSaleSupportRegisterId(),  ModelConst.SALE_SUPPORT_REGISTER);
                Object attachment = saleSupport.getData().getData().get("attachment");

                if (attachment != null  && "[]".equals(String.valueOf(attachment))) {
                    param.put("attachment", saleSupportRecordDto.getAttachmentFormats());
                } else {
                    List<AttachmentFormat> oldAttr = ObjectMapperUtils.readValue(JSON.toJSONString(attachment), AttachmentFormat.class);
                    List<AttachmentFormat> attachmentFormats = saleSupportRecordDto.getAttachmentFormats();
                    attachmentFormats.addAll(oldAttr);
                    param.put("attachment", attachmentFormats);
                }
                param.put("id",saleSupportRecordDto.getSaleSupportRegisterId());
                BoServiceUtils.updateMainBo(ModelConst.SALE_SUPPORT_REGISTER,param);
            }
        }catch (Exception e){
            log.error("售后登记-添加处理记录失败",e);
        }

        return com.alibaba.cola.dto.SingleResponse.buildSuccess();
    }

}
